package com.canbot.u05.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.canbot.u05.R;
import com.canbot.u05.bean.Dance;

import java.util.List;

/**
 * Created by lrh on 2017/3/3.
 */

//适配器的构造
public class HostModePerformAdapter extends BaseAdapter {


        private List<Dance> dList;

        private Context context;

        private LayoutInflater mInflater;


        public HostModePerformAdapter(Context context, List<Dance> dList) {
                super();
                this.context = context;
                mInflater = LayoutInflater.from(context);

                this.dList = dList;
        }

        @Override
        public int getCount() {
                return dList.size();
        }

        @Override
        public Object getItem(int position) {
                return dList.get(position);
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        //然后重写getView
        @Override
        public View getView(final int position, View view, ViewGroup parent) {

                ViewHolder holder = null;
                if (view == null) {
                        holder = new ViewHolder();
                        view = mInflater.inflate(R.layout.hostmode_perform_item, null);
                        holder.imageView = (ImageView) view.findViewById(R.id.image_dance);
                        holder.fileName = (TextView) view.findViewById(R.id.dance_text);
                        holder.checkBox = (CheckBox) view.findViewById(R.id.cb_dance);
                        view.setTag(holder);

                }
                else {
                        holder = (ViewHolder) view.getTag();
                }
                Dance music = dList.get(position);
                holder.fileName.setText(music.getName());
                holder.checkBox.setChecked(music.isChecked());
                holder.imageView.setSelected(music.isChecked());
                Log.e("TAG", "position" + view);

                return view;
        }

        private class ViewHolder {

                private TextView fileName;

                private ImageView imageView;

                private CheckBox checkBox;
        }

        //
        public void updataView(int posi, GridView listView) {
                int visibleFirstPosi = listView.getFirstVisiblePosition();
                int visibleLastPosi = listView.getLastVisiblePosition();


                if (posi >= visibleFirstPosi && posi <= visibleLastPosi) {
                        View view = listView.getChildAt(posi - visibleFirstPosi);
                        ViewHolder holder = (ViewHolder) view.getTag();
                        holder.checkBox.setChecked(dList.get(posi).isChecked());
                        holder.imageView.setSelected(dList.get(posi).isChecked());

                }
                else {

                }
        }


}
